package com.sl.crawler.robot.clientPool;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.sl.crawler.robot.request.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by shiling on 2017/5/26.
 */
public class HtmlUnitClientPool extends BaseClientPool<WebClient> {
    private final static Logger LOG = LoggerFactory.getLogger(HtmlUnitClientPool.class);


    @Override
    public WebClient createClient() {
        WebClient webClient = new WebClient();
        //设置参数
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);

        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        webClient.getCookieManager().setCookiesEnabled(true);//开启cookie管理
        webClient.getOptions().setTimeout(30000);
        return webClient;
    }

    @Override
    public String getUrl(WebClient client, Request request) throws Exception {
        HtmlPage htmlPage = client.getPage(request.getUrl());
        if (htmlPage == null) {
            return "";
        }
        return htmlPage.asXml();
    }

    @Override
    public void closeOne(WebClient client) {
        client.close();
    }
}
